﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using System.Data;

public partial class PM_Bas_Styles : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
        }
    }
    protected void BtnColorGroup(object sender, DirectEventArgs e)
    {
        string megs = mydb.DM_GetExecuteScalarObj("KM_StyleElements_DataPort 0").ToString();
        if (megs == "-1")
        {
            ShowBox("数据导入出错！");
        }
        else
        {
            ShowBox(megs);
        }
    }
    protected void BtnStyleElements(object sender, DirectEventArgs e)
    {
        if (mydb.DM_ExecuteNonQuery("KM_StyleElements_DataPort 1") != -1)
        {
            ShowBox("成分导入成功，请查看恒康系统！");
        }
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        //BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
    protected void MultiUpload1_FileUpload(object sender, FileUploadEventArgs e)
    {
        //System.Threading.Thread.Sleep(3000); // Just for testing
        string path = Server.MapPath("../UploadFile/") + e.FileName;
        e.PostedFile.SaveAs(path);
        DataSet ds = ExcelDataSource(path, ExcelSheetName(path)[0].ToString());
        string sql = "";
        string code = "";
        int orderid = 1;
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            if (code == dr["款号"].ToString())
                orderid++;
            else orderid = 1;
            sql += @" insert into KM_Bas_StyleElements(orderid,StyleCode,MaterialShortName,typeid,ElementsName,ElementsRate,remark) 
                      values('" + orderid.ToString() + "','" + dr["款号"] + "','" + dr["物料简称"] + "','"+dr["类型"]+"','" + dr["成份"] + "','" 
                             + dr["比例"] + "','" + dr["备注"] + "')";
            code = dr["款号"].ToString();
        }
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            this.Store3.DataSource = ds;
            this.Store3.DataBind();
            X.Msg.Notify("导入成功！", "文件名: " + e.FileName).Show();
        }
        else
        {
            X.Msg.Notify("导入失败！", "文件名: " + e.FileName).Show();
        }
        
    }
}